<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="推广链接ID" prop="spreadLinkId">
              <el-input v-model="queryParams.spreadLinkId" placeholder="请输入推广链接ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="推广链接key" prop="spreadLinkKey">
              <el-input v-model="queryParams.spreadLinkKey" placeholder="请输入推广链接key" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="浏览器" prop="browser">
              <el-input v-model="queryParams.browser" placeholder="请输入浏览器" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="访问域名" prop="httpUrl">
              <el-input v-model="queryParams.httpUrl" placeholder="请输入访问域名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备型号" prop="model">
              <el-input v-model="queryParams.model" placeholder="请输入设备型号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备型号名称" prop="modelName">
              <el-input v-model="queryParams.modelName" placeholder="请输入设备型号名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备" prop="pvendor">
              <el-input v-model="queryParams.pvendor" placeholder="请输入设备" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备名称" prop="pvendorName">
              <el-input v-model="queryParams.pvendorName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="访客key" prop="userKey">
              <el-input v-model="queryParams.userKey" placeholder="请输入访客key" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="供应商" prop="vendor">
              <el-input v-model="queryParams.vendor" placeholder="请输入供应商" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['bus:jqtSpreadLinkIp:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['bus:jqtSpreadLinkIp:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['bus:jqtSpreadLinkIp:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['bus:jqtSpreadLinkIp:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="jqtSpreadLinkIpList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="唯一ID" align="center" prop="id" v-if="true" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="推广链接ID" align="center" prop="spreadLinkId" />
        <el-table-column label="推广链接key" align="center" prop="spreadLinkKey" />
        <el-table-column label="浏览器" align="center" prop="browser" />
        <el-table-column label="访问域名" align="center" prop="httpUrl" />
        <el-table-column label="设备型号" align="center" prop="model" />
        <el-table-column label="设备型号名称" align="center" prop="modelName" />
        <el-table-column label="设备" align="center" prop="pvendor" />
        <el-table-column label="设备名称" align="center" prop="pvendorName" />
        <el-table-column label="访客key" align="center" prop="userKey" />
        <el-table-column label="供应商" align="center" prop="vendor" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bus:jqtSpreadLinkIp:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bus:jqtSpreadLinkIp:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改推广链接ip对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="jqtSpreadLinkIpFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="推广链接ID" prop="spreadLinkId">
          <el-input v-model="form.spreadLinkId" placeholder="请输入推广链接ID" />
        </el-form-item>
        <el-form-item label="推广链接key" prop="spreadLinkKey">
          <el-input v-model="form.spreadLinkKey" placeholder="请输入推广链接key" />
        </el-form-item>
        <el-form-item label="浏览器" prop="browser">
          <el-input v-model="form.browser" placeholder="请输入浏览器" />
        </el-form-item>
        <el-form-item label="访问域名" prop="httpUrl">
          <el-input v-model="form.httpUrl" placeholder="请输入访问域名" />
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备型号名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入设备型号名称" />
        </el-form-item>
        <el-form-item label="设备" prop="pvendor">
          <el-input v-model="form.pvendor" placeholder="请输入设备" />
        </el-form-item>
        <el-form-item label="设备名称" prop="pvendorName">
          <el-input v-model="form.pvendorName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="访客key" prop="userKey">
          <el-input v-model="form.userKey" placeholder="请输入访客key" />
        </el-form-item>
        <el-form-item label="供应商" prop="vendor">
          <el-input v-model="form.vendor" placeholder="请输入供应商" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="JqtSpreadLinkIp" lang="ts">
import { listJqtSpreadLinkIp, getJqtSpreadLinkIp, delJqtSpreadLinkIp, addJqtSpreadLinkIp, updateJqtSpreadLinkIp } from '@/api/bus/jqtSpreadLinkIp';
import { JqtSpreadLinkIpVO, JqtSpreadLinkIpQuery, JqtSpreadLinkIpForm } from '@/api/bus/jqtSpreadLinkIp/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const jqtSpreadLinkIpList = ref<JqtSpreadLinkIpVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const jqtSpreadLinkIpFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: JqtSpreadLinkIpForm = {
  id: undefined,
  remark: undefined,
  spreadLinkId: undefined,
  spreadLinkKey: undefined,
  browser: undefined,
  httpUrl: undefined,
  model: undefined,
  modelName: undefined,
  pvendor: undefined,
  pvendorName: undefined,
  userKey: undefined,
  vendor: undefined
}
const data = reactive<PageData<JqtSpreadLinkIpForm, JqtSpreadLinkIpQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    spreadLinkId: undefined,
    spreadLinkKey: undefined,
    browser: undefined,
    httpUrl: undefined,
    model: undefined,
    modelName: undefined,
    pvendor: undefined,
    pvendorName: undefined,
    userKey: undefined,
    vendor: undefined,
    params: {
    }
  },
  rules: {
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    spreadLinkId: [
      { required: true, message: "推广链接ID不能为空", trigger: "blur" }
    ],
    spreadLinkKey: [
      { required: true, message: "推广链接key不能为空", trigger: "blur" }
    ],
    browser: [
      { required: true, message: "浏览器不能为空", trigger: "blur" }
    ],
    httpUrl: [
      { required: true, message: "访问域名不能为空", trigger: "blur" }
    ],
    model: [
      { required: true, message: "设备型号不能为空", trigger: "blur" }
    ],
    modelName: [
      { required: true, message: "设备型号名称不能为空", trigger: "blur" }
    ],
    pvendor: [
      { required: true, message: "设备不能为空", trigger: "blur" }
    ],
    pvendorName: [
      { required: true, message: "设备名称不能为空", trigger: "blur" }
    ],
    userKey: [
      { required: true, message: "访客key不能为空", trigger: "blur" }
    ],
    vendor: [
      { required: true, message: "供应商不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询推广链接ip列表 */
const getList = async () => {
  loading.value = true;
  const res = await listJqtSpreadLinkIp(queryParams.value);
  jqtSpreadLinkIpList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  jqtSpreadLinkIpFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: JqtSpreadLinkIpVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加推广链接ip";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: JqtSpreadLinkIpVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getJqtSpreadLinkIp(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改推广链接ip";
}

/** 提交按钮 */
const submitForm = () => {
  jqtSpreadLinkIpFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateJqtSpreadLinkIp(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addJqtSpreadLinkIp(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: JqtSpreadLinkIpVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除推广链接ip编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delJqtSpreadLinkIp(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('bus/jqtSpreadLinkIp/export', {
    ...queryParams.value
  }, `jqtSpreadLinkIp_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
